<?php
	include_once 'isLogin.php';
	$groupc=new group();
	$id=$_GET['id'];
	$table_group="admin_group";					//角色表
	$table_group_right="admin_group_right";		//角色权限批量表

	//读取一级分类
	$cate1=$commonc->getList("admin_right_class");
	$cate1=$cate1['result'];

	//读取二级分类
	$cate2=$commonc->getList("admin_right");
	$cate2=$cate2['result'];

	if(is_array($cate1)){
		foreach ($cate1 as $k1 => $v1) {
			$cate[$k1]['class_name']=$v1['class_name'];
			if(is_array($cate2)){
				foreach ($cate2 as $k2 => $v2) {
					if($v1['id']==$v2['class_id']){
						$cate[$k1]['child'][$k2]= $v2;
					}
				}
			}
		}
	}
	$smarty->assign('cate',$cate);


	if($_POST['submit']){	
		$right_id=$_POST['right_id'];				
		//批量数组
		if($right_id!='' && $right_id!=null){
			$right_id=substr($right_id,0,-1);
			$right_id=explode(',',$right_id);
			if (is_array($right_id)) {
				foreach ($right_id as $k => $v) {
					$arr[$k]=array('right_id'=>$v,);
				}
			}
		}
		
		$arr_class=array('group_name'=>$_POST['group_name'],);

		if($id!='' && $id!=null){

			$res=$groupc->right_update($id,$arr_class,$arr);	
			$msg = "修改成功！";
			$url = "group_name_add.php?id=".$id;
		}else{
			$res=$groupc->right_add($arr_class,$arr);	
			$msg = "添加成功！";
			$url = "group_name_list.php";
		}
		if($res==false){	
			$msg = "添加失败,请于管理员进行联系";
		}

		
		echo $commonc->submitAlert($msg, $url);
		exit;
	}


	if($id!='' && $id!=null){ 
		$row=$commonc->getInfo($table_group,$id);
		//获取已有权限
		$select_right=$commonc->getList($table_group_right." WHERE group_id=".$row['id']." ORDER BY right_id");
		$select_right=$select_right['result'];
		if (is_array($select_right)) {
			foreach ($select_right as $k1 => $v1) {
				$right_id.=$v1['right_id'].",";	
			}
		}
		//存入隐藏框
		$smarty->assign('right_id',$right_id);
		//修改读取用
		$right_id=substr($right_id,0,-1);
		$row['right_id']=explode(',',$right_id);
	
		$smarty->assign('row',$row);
	}




	
	$smarty->display("group_name_add.html");
?>